﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using LTP.Common;
using WebManager;
using Maticsoft.DBUtility;

namespace BaoJian.WebManager.Manager.VIP
{
    public partial class Edit : System.Web.UI.Page
    {
        string OID = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            OID = HttpContext.Current.Request.QueryString["VIPID"];
            if (OID == null)
            {
                return;
            }
            if (!Page.IsPostBack)
            {
                ShowInfo(OID);
                BindRelatedLink();
            }
        }

        private void ShowInfo(string OID)
        {
            LYQ.BLL.VIPPartner bll = new LYQ.BLL.VIPPartner();
            LYQ.Model.VIPPartner model = bll.GetModel(OID);
            //this.txtSID.Text = model.SID.ToString();
            //this.lblOID.Text = model.OID;
            this.txtName.Text = model.Name;
            this.txtTel.Text = model.Tel;
            this.txtAddress.Text = model.Address;
            this.txtQQ.Text = model.QQ;
            this.txtMobile.Text = model.Mobile;
            this.txtFlashPath.Text = model.FlashPath;
            this.txtIntroduce.Text = model.Introduce;
            this.txtProductWhere.Text = model.ProductWhere;
            this.txtPageKeyWord.Text = model.PageKeyWord;
            this.txtURL.Text = model.URL;
            txtURL.Text = model.URL;
            chkInside.Checked = model.Inside;

        }
        private void BindRelatedLink()
        {
            LYQ.BLL.RelatedLink bll = new LYQ.BLL.RelatedLink();
            DataSet ds = bll.GetList("oid='" + OID + "'");
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
        }
        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            (Master.FindControl("lblTitle") as Label).Text = "信息修改";
        }

        protected void Save_Click(object sender, EventArgs e)
        {
            string strErr = "";
            //if (!PageValidate.IsNumber(txtSID.Text))
            //{
            //    strErr += "SID不是数字！\\n";
            //}
            if (this.txtName.Text == "")
            {
                strErr += "Name不能为空！\\n";
            }
            //if (this.txtTel.Text == "")
            //{
            //    strErr += "Tel不能为空！\\n";
            //}
            //if (this.txtAddress.Text == "")
            //{
            //    strErr += "Address不能为空！\\n";
            //}
            //if (this.txtQQ.Text == "")
            //{
            //    strErr += "QQ不能为空！\\n";
            //}
            //if (this.txtMobile.Text == "")
            //{
            //    strErr += "Mobile不能为空！\\n";
            //}
            //if (this.txtFlashPath.Text == "")
            //{
            //    strErr += "FlashPath不能为空！\\n";
            //}
            //if (this.txtIntroduce.Text == "")
            //{
            //    strErr += "Introduce不能为空！\\n";
            //}
            //if (this.txtProductWhere.Text == "")
            //{
            //    strErr += "ProductWhere不能为空！\\n";
            //}
            //if (this.txtPageKeyWord.Text == "")
            //{
            //    strErr += "PageKeyWord不能为空！\\n";
            //}

            if (strErr != "")
            {
                //MessageBox.Show(this, strErr);
                Label lable = (Master.FindControl("lblTitle") as Label);
                lable.Text = strErr;
                lable.Visible = true;
               
                return;
            }
            //int SID = int.Parse(this.txtSID.Text);
            string Name = this.txtName.Text;
            string Tel = this.txtTel.Text;
            string Address = this.txtAddress.Text;
            string QQ = this.txtQQ.Text;
            string Mobile = this.txtMobile.Text;
            string FlashPath = this.txtFlashPath.Text;
            string Introduce = this.txtIntroduce.Text;
            string ProductWhere = this.txtProductWhere.Text;
            string PageKeyWord = this.txtPageKeyWord.Text;
            string URL = this.txtURL.Text;

            LYQ.BLL.VIPPartner bll = new LYQ.BLL.VIPPartner();
            LYQ.Model.VIPPartner model = bll.GetModel(OID);

            model.Name = Name;
            model.Tel = Tel;
            model.Address = Address;
            model.QQ = QQ;
            model.Mobile = Mobile;
            model.FlashPath = FlashPath;
            model.Introduce = Introduce;
            model.ProductWhere = ProductWhere;
            model.PageKeyWord = PageKeyWord;
            model.URL = URL;
            model.Inside = chkInside.Checked;


            bll.Update(model);
            string url = URLUtility.UrlBase + @"/WebManager/manager/vip/List.aspx";
            Page.Response.Redirect(url);
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            string url = URLUtility.UrlBase + @"/WebManager/manager/vip/List.aspx";
            Page.Response.Redirect(url);
        }

        protected void AddRelatedLink_Click(object sender, EventArgs e)
        {
            LYQ.BLL.RelatedLink bll = new LYQ.BLL.RelatedLink();
            LYQ.Model.RelatedLink model = new LYQ.Model.RelatedLink();
            model.OID = OID;
            model.Name = Name.Text;
            model.Link = Link.Text;
            model.SID = bll.GetMaxId();
            bll.Add(model);
            BindRelatedLink();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            BindRelatedLink();

        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            BindRelatedLink();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {            
            string sid = GridView1.Rows[e.RowIndex].Cells[0].Text;
            string name = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TName")).Text;
            string link = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TLink")).Text;        
          

            LYQ.BLL.RelatedLink bll = new LYQ.BLL.RelatedLink();
            LYQ.Model.RelatedLink model = new LYQ.Model.RelatedLink();
            model.SID = Convert.ToInt32(sid);
            model.Name = name;
            model.Link = link;
            model.OID = OID;
            bll.Update(model);

            GridView1.EditIndex = -1;
            BindRelatedLink();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string sid = GridView1.Rows[e.RowIndex].Cells[0].Text;
            LYQ.BLL.RelatedLink bll = new LYQ.BLL.RelatedLink();
            bll.Delete(Convert.ToInt32(sid));
            BindRelatedLink();


        }
    }
}
